import java.util.Arrays;

public class Test21 {

        public int shortestDistance(String[] wordsDict, String word1, String word2) {
            int n = wordsDict.length;
//
            int a1 =0;
            for( a1 = 0;a1<n ; a1++){
                if(wordsDict[a1].equals(word1)==true){
                    break;
                }
            }
//
            int a2 =0;
            for( a2 = n-1;a2>=0 ; a2--){
                if(wordsDict[a2].equals(word1)==true){
                    break;
                }
            }
//
            int b1 =0;
            for(b1 =0; b1<n ; b1++){
                if(wordsDict[b1].equals(word2)==true){
                    break;
                }
            }
//
            int b2 = 0;
            for(b2= n-1; b2>=0 ; b2--){
                if(wordsDict[b2].equals(word2)==true){
                    break;
                }
            }
            int[] value = new int[4];
             value[0] = Math.abs(a1-b1);
             value[1] = Math.abs(a1-b2);
             value[2] = Math.abs(a2-b1);
            value[3] = Math.abs(a2-b1);
            Arrays.sort(value);
            int c = value[0];
            return c;
        }
    }

